﻿using System;
using NHibernate.Sidekick.ConsoleTests.Domain.Entities;
using NHibernate.Sidekick.Services;
using NHibernate.Sidekick.Services.Data;

namespace NHibernate.Sidekick.ConsoleTests
{
    public class Program
    {
        public static void Main()
        {
            Setup.NHibernate.Initialize("testDb.db3");
            
            InsertTestValues();
            PrintTestValues();
            Console.Write("\nPress any key to continue...");
            Console.ReadKey(true);
        }

        private static void PrintTestValues()
        {
            foreach (var store in NSession.Query<Store>())
            {
                Console.WriteLine(string.Format("{0} ({1} {2})",
                                                store.Name,
                                                store.Staff.Count,
                                                store.Staff.Count != 1 ? "employees" : "employee"));

                Console.WriteLine("\t*** Products ***");
                foreach (var product in store.Products)
                {
                    Console.WriteLine(string.Format("\t- {0} - {1:c}", product.Name, product.Price));
                }
                
                Console.WriteLine();

                Console.WriteLine("\t*** Employees ***");
                foreach (var employee in store.Staff)
                {
                    Console.WriteLine(string.Format("\t- {0} {1} ({2})", employee.FirstName, employee.LastName, employee.Telephone).TrimEnd());
                }

                Console.WriteLine();
            }
        }

        private static void InsertTestValues()
        {
            var store = new Store { Name = "Askview Universe" };

            var product1 = new Product { Name = "Mallrats", Price = 19.99 };
            product1.StoresStockedIn.Add(store);

            var product2 = new Product { Name = "Gigli", Price = DateTime.Now.Millisecond };
            product2.StoresStockedIn.Add(store);

            var employee = new Employee
            {
                FirstName = "Silent",
                LastName = "Bob",
                Telephone = "809-123-4567",
                Store = store,
                Birthday = DateTime.Now
            };
           
            store.AddProduct(product1);
            store.AddProduct(product2);
            store.AddEmployee(employee);
            Service<Store>.Instance.Save(store);
            NSession.Session.Flush();
        }
    }
}
